// 函数

// 函数类型
function add(x: number, y: number): number {
  return x + y
}

// 完整的函数类型书写方式
const myAdd: (x: number, y: number) => number = (x: number, y: number): number => {
  return x + y
}

const myAdd1: (x: number, y: number) => number = function (x: number, y: number): number {
  return x + y
}

// 可选参数和默认参数
function buildName(firstName: string, lastName?: string): string {
  return lastName ? firstName + '_' + lastName : firstName
}

// 剩余参数
function info(x: string, ...args: string[]) {
  console.log(x, args)
}
info('abc', 'c', 'b', 'a')

// 函数重载
function selfStr(x: string, y: string): string
function selfStr(x: number, y: number): number

function selfStr(x: string | number, y: string | number): string | number {
  if (typeof x === 'string' && typeof y === 'string') {
    return x + y
  } else if (typeof x === 'number' && typeof y === 'number') {
    return x + y
  } else {
    return 'Error'
  }
}


